package myJs.projectCps.enrichBarPlot

import cats.effect.SyncIO
import com.softwaremill.quicklens._
import myJs.cps._
import myJs.tool.Tool._

import scala.collection.immutable.SeqMap
import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._

/** Created by yz on 3/11/2020
 */
object FontCp {

  val plotO=Cp.plotO

  case class Props(
    basicData: plotO.FontData,
    basicDataChange: (plotO.FontData => plotO.FontData) => SyncIO[Unit]
  ){
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>

    object FTool {

      def strChange(f: PathLazyModify[plotO.FontData, String]) = (v: String) => {
        props.basicDataChange {
          f.setTo(v)
        }
      }

    }

    val basicData = props.basicData
    val fontFamilyMap = SeqMap("Times" -> "Times New Roman（serif）", "ArialMT" -> "Arial（sans）",
      "mono" -> "Courier（mono）", "SimSun" -> "宋体（SimSun）")

    Fragment(
      SelectInlineCp.Props(
        value = basicData.fontFamily,
        label = messages("font"),
        name = "fontFamily",
        optionMap = fontFamilyMap,
        labelWidth = 4,
        inputWidth = 8,
        onChange = FTool.strChange(modifyLens[plotO.FontData](_.fontFamily))
      ).render,
      InputInlineCp.Props(
        label = messages("mainTitleSize"),
        name = "mainTitleFont",
        labelWidth = 4,
        inputWidth = 8,
        value = basicData.mainTitleFont,
        onChange = FTool.strChange(modifyLens[plotO.FontData](_.mainTitleFont))
      ).render,
      InputInlineCp.Props(
        label = messages("xaxisTitleSize"),
        name = "xTitleFont",
        labelWidth = 4,
        inputWidth = 8,
        value = basicData.xTitleFont,
        onChange = FTool.strChange(modifyLens[plotO.FontData](_.xTitleFont))
      ).render,
      InputInlineCp
        .Props(
          label = messages("xaxisTextSize"),
          name = "xFont",
          labelWidth = 4,
          inputWidth = 8,
          value = basicData.xFont,
          onChange = FTool.strChange(modifyLens[plotO.FontData](_.xFont))
        )
        .render,
      InputInlineCp
        .Props(
          label = messages("yAxisTextSize"),
          name = "yFont",
          labelWidth = 4,
          inputWidth = 8,
          value = basicData.yFont,
          onChange = FTool.strChange(modifyLens[plotO.FontData](_.yFont))
        )
        .render,
      InputInlineCp.Props(
        label = messages("legendTitleContent"),
        name = "legendTitle",
        labelWidth = 4,
        inputWidth = 8,
        value = basicData.legendTitle,
        onChange = FTool.strChange(modifyLens[plotO.FontData](_.legendTitle))
      ).render,
      InputInlineCp
        .Props(
          label = messages("legendTitleSize"),
          name = "legendTitleFont",
          labelWidth = 4,
          inputWidth = 8,
          value = basicData.legendTitleFont,
          onChange = FTool.strChange(modifyLens[plotO.FontData](_.legendTitleFont))
        )
        .render,
      InputInlineCp
        .Props(
          label = messages("legendTextSize"),
          name = "legendFont",
          labelWidth = 4,
          inputWidth = 8,
          value = basicData.legendFont,
          onChange = FTool.strChange(modifyLens[plotO.FontData](_.legendFont))
        )
        .render
    )

  }

}
